package com.xindong.paking.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xindong.paking.dtos.PageResult;
import com.xindong.paking.entity.TbRole;
import com.xindong.paking.mapper.TbRoleMapper;
import com.xindong.paking.service.TbRoleService;
import com.xindong.paking.utils.Result;
import com.xindong.paking.utils.ResultStatus;
import com.xindong.paking.utils.UserInfoUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author huangshengce
 * @since 2023-01-08
 */
@Service
public class TbRoleServiceImpl extends ServiceImpl<TbRoleMapper, TbRole> implements TbRoleService {

    @Autowired
    private TbRoleMapper mapper;


    /**
     * 增加
     * @param adminUser
     * @return
     */
    @Override
    public Result addAdmin(TbRole adminUser) {
        // 判断用户是否存在
        QueryWrapper<TbRole> tbRoleQueryWrapper = new QueryWrapper<>();
        tbRoleQueryWrapper.eq("account", adminUser.getAccount());
        TbRole tbRole = mapper.selectOne(tbRoleQueryWrapper);
        if (tbRole != null) {
            return new Result(ResultStatus.USER_HAS_EXISTED);
        }

        // 判断手机号是否存在
        tbRoleQueryWrapper = new QueryWrapper<>();
        tbRoleQueryWrapper.eq("phone", adminUser.getPhone());
        tbRole = mapper.selectOne(tbRoleQueryWrapper);
        if (tbRole != null) {
            return new Result(ResultStatus.USER_PHONE_EXISTED);
        }


        // 用户权限
        adminUser.setRole(0);
        mapper.insert(adminUser);
        return new Result(ResultStatus.SUCCESS);
    }


    /**
     * 删除
     * @param id
     * @return
     */
    @Override
    public Result deleteRole(Integer id) {
        mapper.deleteById(id);
        return Result.success("删除成功", null);
    }


    /**
     * 更新
     *
     * @param tbRole
     * @return
     */
    @Override
    public Result updateRole(TbRole tbRole) {
        // 当前操作用户信息
        Map<String, Object> userInfo = UserInfoUtil.getUserInfo();
        System.out.println("userInfo = " + userInfo);
        // 操作更新的用户id
//        tbRole.setId((Integer) userInfo.get("id"));
        System.out.println((Integer) userInfo.get("id"));

        // 判断是否为修改密码
        String password = tbRole.getPassword();
        /*// 密码加密
        if (password != null) {
            String gensalt = BCrypt.gensalt();
            String hashpw = BCrypt.hashpw(password, gensalt);
            tbRole.setPassword(hashpw);
        }*/
        mapper.updateById(tbRole);
        return Result.success("更新成功", null);
    }



    /**
     * 分页查询
     *
     * @param page     当前页
     * @param pageSize 每页大小
     * @return
     */
    @Override
    public Result list(Integer page, Integer pageSize) {
        // 设置分页
        IPage<TbRole> ipage = new Page<>(page, pageSize);

        // 进行分页查询
        IPage<TbRole> tbUserIPage = mapper.selectPage(ipage, null);

        // 获取分页后数据，并把密码设为空
        List<TbRole> records = tbUserIPage.getRecords();
        for (TbRole record : records) {
            record.setPassword(null);
        }

        // 分页返回数据
        PageResult result = new PageResult(records, tbUserIPage.getTotal(), page, pageSize);
        return Result.success("获取数据成功", result);
    }
}
